/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

class StaticHttpServletRequestTranslator extends TranslatorBase {

   static JavaType javaType;

   StaticHttpServletRequestTranslator() {
      try {
         fields.put( "BASIC_AUTH",       JavaType.value_translator( "java.lang.String" ).literal( "\"BASIC\"" ) );
         fields.put( "FORM_AUTH",        JavaType.value_translator( "java.lang.String" ).literal( "\"FORM\"" ) );
         fields.put( "CLIENT_CERT_AUTH", JavaType.value_translator( "java.lang.String" ).literal( "\"CLIENT_CERT\"" ) );
         fields.put( "DIGEST_AUTH",      JavaType.value_translator( "java.lang.String" ).literal( "\"DIGEST\"" ) );
      }
      catch( TranslatorException e ) {
         System.err.println( "Ups - none string translator ? " + e );
      }
   }

   public String name() {
      return "javax.servlet.http.HttpServletRequest";
   }

   public String php_name() {
      return "none direct equivalent";
   }

}

/**
 * @composed 1 - 1 HttpServletRequestValueTranslator
 */
class HttpServletRequestTranslator extends StaticHttpServletRequestTranslator implements ITypeTranslator {

   private static HttpServletRequestValueTranslator value_translator = new HttpServletRequestValueTranslator();

   static {
      ITypeTranslator tr = new HttpServletRequestTranslator();
      JavaType.add_type_translator( tr );
   }

   private HttpServletRequestTranslator() {}

   public IValueTranslator value_translator() {
      return value_translator;
   }
}

class HttpServletRequestValueTranslator extends StaticHttpServletRequestTranslator implements IValueTranslator {

   JavaType myJavaType() throws TranslatorException {
      return JavaType.value_type( "javax.servlet.http.HttpServletRequest" );
   }

   public Translation translate_getAuthType(Translation ... args) throws TranslatorException {
      return translate("getAuthType", "( isset($_SERVER[\"PHP_AUTH_USER\"]) ? \"BASIC\" : null )", 0, JavaType.string(), null, true, args, false);
   }

   public Translation translate_getContentLength(Translation ... args) throws TranslatorException {
      return translate("getContentLength", "( isset($_SERVER[\"CONTENT_LENGTH\"]) ? $_SERVER[\"CONTENT_LENGTH\"] : -1 )", 0, JavaType.integer(), null, true, args, false);
   }

   public Translation translate_getContentType(Translation ... args) throws TranslatorException {
      return translate("getContentType", "$_SERVER[\"CONTENT_TYPE\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getContextPath(Translation ... args) throws TranslatorException {
      return translate("getContextPath", "implode(\"/\", array_slice(explode(\"/\", $_SERVER[\"SCRIPT_NAME\"]), 0, substr_count($_SERVER[\"SCRIPT_NAME\"], \"/\") ))", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getHeader(Translation ... args) throws TranslatorException {
      return translate("getHeader", "$_REQUEST[%2$s]", 1, JavaType.string(), new JavaType[]{ JavaType.string() }, true, args);
   }

   public Translation translate_getIntHeader(Translation ... args) throws TranslatorException {
      return translate("getIntHeader", "(int) $_REQUEST[%2$s]", 1, JavaType.integer(), new JavaType[]{ JavaType.string() }, true, args, false);
   }

   public Translation translate_getLocalAddr(Translation ... args) throws TranslatorException {
      return translate("getLocalAddr", "$_SERVER[\"SERVER_ADDR\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getLocalName(Translation ... args) throws TranslatorException {
      return translate("getLocalName", "$_SERVER[\"SERVER_NAME\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getLocalPort(Translation ... args) throws TranslatorException {
      return translate("getLocalPort", "$_SERVER[\"SERVER_PORT\"]", 0, JavaType.integer(), null, true, args);
   }

   public Translation translate_getMethod(Translation ... args) throws TranslatorException {
      return translate("getMethod", "$_SERVER[\"REQUEST_METHOD\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getParameter(Translation ... args) throws TranslatorException {
      return translate("getParameter", "$_REQUEST[%2$s]", 1, JavaType.string(), new JavaType[]{ JavaType.string() }, true, args);
   }

   public Translation translate_getPathInfo(Translation ... args) throws TranslatorException {
      return translate("getPathInfo", "$_SERVER[\"PATH_INFO\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getPathTranslated(Translation ... args) throws TranslatorException {
      return translate("getPathTranslated", "$_SERVER[\"PATH_TRANSLATED\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getProtocol(Translation ... args) throws TranslatorException {
      return translate("getProtocol", "$_SERVER[\"SERVER_PROTOCOL\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getQueryString(Translation ... args) throws TranslatorException {
      return translate("getQueryString", "$_SERVER[\"QUERY_STRING\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getRemoteAddr(Translation ... args) throws TranslatorException {
      return translate("getRemoteAddr", "$_SERVER[\"REMOTE_ADDR\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getRemoteHost(Translation ... args) throws TranslatorException {
      return translate("getRemoteHost", "$_SERVER[\"REMOTE_HOST\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getRemotePort(Translation ... args) throws TranslatorException {
      return translate("getRemotePort", "$_SERVER[\"REMOTE_PORT\"]", 0, JavaType.integer(), null, true, args);
   }

   public Translation translate_getRemoteUser(Translation ... args) throws TranslatorException {
      return translate("getRemoteUser", "( isset($_SERVER[\"REMOTE_USER\"]) ? $_SERVER[\"REMOTE_USER\"] : $_SERVER[\"PHP_REMOTE_USER\"] )", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getRequestedSessionId(Translation ... args) throws TranslatorException {
      return translate("getRequestedSessionId", "( isset( $_REQUEST[session_name()] ) ? $_REQUEST[session_name()] : null )", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getRequestURI(Translation ... args) throws TranslatorException {
      return translate("getRequestURI", "$_SERVER[\"REQUEST_URI\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getServerName(Translation ... args) throws TranslatorException {
      return translate("getServerName", "$_SERVER[\"SERVER_NAME\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_getServletPath(Translation ... args) throws TranslatorException {
      return translate("getServletPath", "$_SERVER[\"SCRIPT_NAME\"]", 0, JavaType.string(), null, true, args);
   }

   public Translation translate_isRequestedSessionIdFromCookie(Translation ... args) throws TranslatorException {
      return translate("isRequestedSessionIdFromCookie", "isset( $_COOKIE[session_name()] )", 0, JavaType.bool(), null, true, args);
   }

   public Translation translate_isRequestedSessionIdFromURL(Translation ... args) throws TranslatorException {
      return translate("isRequestedSessionIdFromURL", "isset( $_REQUEST[session_name()] )", 0, JavaType.bool(), null, true, args);
   }

   public Translation translate_isRequestedSessionIdFromUrl(Translation ... args) throws TranslatorException {
      return translate("isRequestedSessionIdFromUrl", "isset( $_REQUEST[session_name()] )", 0, JavaType.bool(), null, true, args);
   }

   public Translation translate_removeAttribute(Translation ... args) throws TranslatorException {
      return translate("removeAttribute", "unset( $_REQUEST[%2$s] )", 1, JavaType.voidType(), new JavaType[]{ JavaType.string() }, true, args);
   }

   public Translation translate_setAttribute(Translation ... args) throws TranslatorException {
      return translate("setAttribute", "$_REQUEST[%2$s] = %3$s", 2, JavaType.voidType(), new JavaType[]{ JavaType.string(), JavaType.string() }, true, args);
   }
}
